package com.yw.atp.action;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yw.atp.model.Department;
import com.yw.atp.model.Profession;
import com.yw.atp.model.Staff;
import com.yw.atp.service.DepartmentService;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.service.ProfessionService;
import com.yw.atp.service.StaffService;
import com.yw.atp.util.StringUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("staffAction")
public class StaffAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
    private PersistenceService service;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private ProfessionService professionService;
	@Autowired
	private StaffService StaffService;
	HttpServletRequest request;
	public List<Staff> staffList;
	public String staffIds;
	public String deprtIds;
	public String persIds;
	public Staff staff;
	public Department deprt;
	public Profession profe;
	public List<Department> departmentList;
	public List<Profession> professionList;
	public String res;
	public int start = 0;
	public int limit = 20;
	public String loadAll = "F";
	
	public String list(){
		
		if(staff==null){
			staffList = service.findAll(Staff.class,0);
		}else{
			staffList = StaffService.getStaffs(staff,profe.getName());
		}
		JSONObject js = new JSONObject();
		try {
			if(staffList!=null&&staffList.size()>0){
				js.put("totalCount",staffList.size());
				if("T".equals(loadAll)){
					js.put("topics",Staff.capture(staffList));
				}else{
					int len = staffList.size();
					start = start>(len-1)?(len-1):start;
					limit = limit>(len-start)?(len):(start+limit);
					js.put("topics",Staff.capture(staffList.subList(start,limit)));
				}
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String add(){
		if(StringUtil.checkStaff(staff)){
			if(staff.getId()!=null&&staff.getId().intValue()>0){
				staff.setCreatedAt(new Date());
			}
			String[] ids;
			if(persIds!=null){
				ids = persIds.split(","); 
				if(ids!=null&&ids.length>0){
					Set<Profession> sList = new HashSet<Profession>();
					for(int i=0;i<ids.length;i++){
						int id ;
						try{id = Integer.valueOf(ids[i]);}catch(Exception e){id = 0;}
						if(ids[i]!=null&&id>0){
							Profession s = new Profession();
							s.setId(id);
							s = service.load(s);
							if(s!=null&&s.getId()!=null&&s.getId().intValue()>0){
								sList.add(s);
							}
						}
					}
					if(sList.size()>0){
						staff.setProfessions(sList);
					}
				}
			}
			service.save(staff);
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String del(){
		if(staffIds!=null&&!"".equals(staffIds)){
			service.remove(Staff.class.getName(),staffIds);
		}
		return SUCCESS;
	}
	
	public String addDeprt(){
		if(StringUtil.checkDepartment(deprt)){
			if(deprt.getId()!=null&&deprt.getId().intValue()>0){
				deprt.setCreatedAt(new Date());
			}
			service.save(deprt);
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String deprtList(){
		if(deprt==null||deprt.getName()==null){
			departmentList = service.findAll(Department.class,0);
		}else{
			departmentList = departmentService.getDepartments(deprt);
		}
		JSONObject js = new JSONObject();
		try {
			if(departmentList!=null&&departmentList.size()>0){
				js.put("totalCount",departmentList.size());
				if("T".equals(loadAll)){
					js.put("topics",Department.capture(departmentList));
				}else{
					int len = departmentList.size();
					start = start>(len-1)?(len-1):start;
					limit = limit>(len-start)?(len):(start+limit);
					js.put("topics",Department.capture(departmentList.subList(start,limit)));
				}
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String delDeprt(){
		if(deprtIds!=null&&!"".equals(deprtIds)){
			service.remove(Department.class.getName(),deprtIds);
		}
		return SUCCESS;
	}
	
	public String addPers(){
		if(StringUtil.checkProfession(profe)){
			if(profe.getId()!=null&&profe.getId().intValue()>0){
				profe.setCreatedAt(new Date());
			}
			service.save(profe);
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String persList(){
		if(profe==null||profe.getName()==null){
			professionList = service.findAll(Profession.class,0);
		}else{
			professionList = professionService.getProfessions(profe);
		}
		JSONObject js = new JSONObject();
		try {
			if(professionList!=null&&professionList.size()>0){
				js.put("totalCount",professionList.size());
				if("T".equals(loadAll)){
					js.put("topics",Profession.capture(professionList));
				}else{
					int len = professionList.size();
					start = start>(len-1)?(len-1):start;
					limit = limit>(len-start)?(len):(start+limit);
					js.put("topics",Profession.capture(professionList.subList(start,limit)));
				}
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String delPers(){
		if(persIds!=null&&!"".equals(persIds)){
			service.remove(Profession.class.getName(),persIds);
		}
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public PersistenceService getService() {
		return service;
	}
	public void setService(PersistenceService service) {
		this.service = service;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public List<Staff> getStaffList() {
		return staffList;
	}
	public void setStaffList(List<Staff> staffList) {
		this.staffList = staffList;
	}
	public Staff getStaff() {
		return staff;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	public Department getDeprt() {
		return deprt;
	}
	public void setDeprt(Department deprt) {
		this.deprt = deprt;
	}
	public Profession getProfe() {
		return profe;
	}
	public void setProfe(Profession profe) {
		this.profe = profe;
	}
	public List<Department> getDepartmentList() {
		return departmentList;
	}
	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}
	public List<Profession> getProfessionList() {
		return professionList;
	}
	public void setProfessionList(List<Profession> professionList) {
		this.professionList = professionList;
	}
	public String getRes() {
		return res;
	}
	public void setRes(String res) {
		this.res = res;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	public ProfessionService getProfessionService() {
		return professionService;
	}
	public void setProfessionService(ProfessionService professionService) {
		this.professionService = professionService;
	}
	public StaffService getStaffService() {
		return StaffService;
	}
	public void setStaffService(StaffService staffService) {
		StaffService = staffService;
	}
	
	public String getStaffIds() {
		return staffIds;
	}
	public void setStaffIds(String staffIds) {
		this.staffIds = staffIds;
	}
	public String getDeprtIds() {
		return deprtIds;
	}
	public void setDeprtIds(String deprtIds) {
		this.deprtIds = deprtIds;
	}
	public String getPersIds() {
		return persIds;
	}
	public void setPersIds(String persIds) {
		this.persIds = persIds;
	}
	public String getLoadAll() {
		return loadAll;
	}
	public void setLoadAll(String loadAll) {
		this.loadAll = loadAll;
	}
}
